﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RendicionGastos.Models;
using System.Text;
using System.Web.Script.Serialization;
using System.IO;
using System.Net;

namespace RendicionGastos.Controllers
{
        [Authorize(Roles = "Administrador")]
    public class GastoController : Controller
    {

        public ActionResult Index()
        {
            // Prueba de creación de alumno vía HTTP POST
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:3181/Gastos.svc/Gastos");
            req.Method = "GET";

            var res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string gastoJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<GastoModel> gastoListado = js.Deserialize<List<GastoModel>>(gastoJson);

            return View(gastoListado);
        }

        public ActionResult Details(int id)
        {
            // Prueba de creación de alumno vía HTTP POST
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:3181/Gastos.svc/Gastos" + "/" + id);
            req.Method = "GET";

            var res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string gastoJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            GastoModel gastoListado = js.Deserialize<GastoModel>(gastoJson);

            return View(gastoListado);
        }

        public ActionResult crear()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult crear(FormCollection collection)
        {
            try
            {
                string estado = "E";
                // Prueba de creación de alumno vía HTTP POST
                string postdata = "{\"Codigo\":\""+collection["codigo"]+"\",\"Nombre\":\""+collection["nombre"]+"\",\"monto\":"+collection["monto"]+",\"estado\":\""+estado+"\"}"; //JSON
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest
                    .Create("http://localhost:3181/Gastos.svc/Gastos");
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string gastoJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                GastoModel gastoCreado = js.Deserialize<GastoModel>(gastoJson);


                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Gasto/Edit/5
 
        public ActionResult Edit(int id)
        {
            // Prueba de creación de alumno vía HTTP POST
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:3181/Gastos.svc/Gastos" + "/" + id);
            req.Method = "GET";

            var res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string gastoJson = reader.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            GastoModel gastoListado = js.Deserialize<GastoModel>(gastoJson);

            return View(gastoListado);
        }

        //
        // POST: /Gasto/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                string estado = "E";
                // Prueba de creación de alumno vía HTTP POST
                string postdata = "{\"Codigo\":\"" + collection["codigo"] + "\",\"Nombre\":\"" + collection["nombre"] + "\",\"monto\":" + collection["monto"] + ",\"estado\":\"" + estado + "\"}"; //JSON
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest
                    .Create("http://localhost:3181/Gastos.svc/Gastos");
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string gastoJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                GastoModel gastoCreado = js.Deserialize<GastoModel>(gastoJson);


                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Gasto/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Gasto/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
